<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\finance\model;

use app\cassification\model\ClassificationDictionary;
use plugin\saiadmin\basic\BaseNormalModel;
use app\financeInstitution\model\financeInstitution;

/**
 * 金融列表模型
 */
class finance extends BaseNormalModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'finance_id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'eb_finance';
    protected $append = ['finance_category_text','finance_institution_text'
    ,'product_tags_text'];



    
    /**
     * 开始时间 搜索
     */
    public function searchStartTimeAttr($query, $value)
    {
        $query->whereTime('start_time', 'between', $value);
    }
    
    /**
     * 结束时间 搜索
     */
    public function searchEndTimeAttr($query, $value)
    {
        $query->whereTime('end_time', 'between', $value);
    }
    
    /**
     * 金融标题 搜索
     */
    public function searchFinanceTitleAttr($query, $value)
    {
        $query->where('finance_title', 'like', '%'.$value.'%');
    }

    public function getFinanceCategoryTextAttr($value,$data)
    {
        if(isset($data['finance_category'])){
            return    ClassificationDictionary::where('dictionary_id', $data['finance_category'])->value('classification_name_zh');
        }
        return '';
    }

    public function getFinanceInstitutionTextAttr($value,$data)
    {
        if(isset($data['finance_institution'])){
            return    financeInstitution::where('institution_id', $data['finance_institution'])->value('institution_name');
        }
        return '';
    }
    public function getProductTagsAttr($value,$data)
    {
        if(isset($data['product_tags'])){
            return   json_decode($data['product_tags'],true);
        }
        return '';
    }
       public function setProductTagsAttr($value,$data)
    {
        if(isset($data['product_tags'])){
            return   json_encode($data['product_tags']);
        }
        return '';
    }
     public function getProductTagsTextAttr($value,$data)
    {
        if(isset($data['product_tags'])){
            if($data['product_tags'])$data['product_tags']=json_decode($data['product_tags'],true);
             $attr=ClassificationDictionary::whereIn('dictionary_id', $data['product_tags'])->column('classification_name_zh');
            return  empty($attr)?'':implode(',',$attr);

        }
        return '';
    }
}
